package cn.gailvlun.gll.chatkit.dialog;

import android.content.Context;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.blankj.utilcode.util.Utils;
import com.github.markzhai.recyclerview.MultiTypeAdapter;

import java.util.List;

import cn.gailvlun.gll.chatkit.R;
import cn.gailvlun.gll.chatlib.dialog.Dialog;
import cn.gailvlun.gll.util.DateUtil;
import cn.gailvlun.gll.util.LogUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.util.SPUtil;



public class DialogListAdapter extends MultiTypeAdapter {

    public final static int SYSTEM_NOTIFICATION = 0;
    public final static int FRIEND_NOTIFICATION = 1;
    public final static int DIALOG = 2;

    public DialogListAdapter(Context context) {
        super(context);
        addViewTypeToLayoutMap(SYSTEM_NOTIFICATION, R.layout.item_system_notification);
        addViewTypeToLayoutMap(FRIEND_NOTIFICATION, R.layout.item_friend_noti);
        addViewTypeToLayoutMap(DIALOG, R.layout.item_dialog_list);
    }

    public static class Util {

        public static Object getAvatar(Dialog item) {
            if (!item.isValid()) {
                return "";
            }
            if (item.isRoom()) {
                return item.getRoom().getPic();
            } else {
                return item.getProfile().getAvatar();
            }
        }

        public static String getName(Dialog item) {
            if (!item.isValid()) {
                return "";
            }
            if (item.isRoom()) {
                return item.getRoom().getName();
            } else {
                return item.getProfile().getName();
            }
        }

        public static int getLatestMessageStatus(Dialog item) {
            if (!item.isValid()) {
                return View.GONE;
            }
            switch (item.getMessage().getMessageStatus()) {
                case FAIL_RESEND:
                case FAIL_UPLOAD:
                    return View.VISIBLE;
                default:
                    return View.GONE;
            }
        }

        public static String PRE_HAS_EXIT_AT_MESSAGE = "PRE_HAS_EXIT_AT_MESSAGE";
        
        public static SpannableString getLatestMessage(final Dialog item) {
            if (!item.isValid()) {
                return null;
            }
            SpannableStringBuilder sbb = new SpannableStringBuilder();
            if (item.getDraft() != null) {
                sbb.append("[草稿] " + item.getDraft())
                        .setSpan(new ForegroundColorSpan(
                                        Utils.getApp().getResources().getColor(R.color.carnation)),
                                0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                return SpannableString.valueOf(sbb);
            }
            switch (item.getMessage().getType()) {
                case text: {
                    boolean atMe = false;
                    if (item.isRoom()) {
                        List<Integer> atWhomList = item.getMessage().getAtWhomList();
                        for (Integer atWhom : atWhomList) {
                            if (atWhom.equals(ProfileUtil.getProfile().getId()) && item.getId().equals(item.getMessage().getDialogId())) {
                                atMe = true;
                                break;
                            }
                        }
                    }

                    boolean hasExitAtMessage = SPUtil.getBoolean(SPUtil.USER, PRE_HAS_EXIT_AT_MESSAGE + "-" + (item.isRoom() ? item.getRoom().getId() : ""));
                    if (item.getUnreadCount() > 0 && (atMe || item.getMessage().isAtAll())) {
                        SPUtil.put(SPUtil.USER, PRE_HAS_EXIT_AT_MESSAGE + "-" + item.getRoom().getId(), true);
                        if (atMe) {
                            sbb.append("[有人@我] " + item.getMessage().getSender().getName() + ": " + item.getMessage().getText())
                                    .setSpan(new ForegroundColorSpan(
                                                    Utils.getApp().getResources().getColor(R.color.carnation)),
                                            0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                        } else if (item.getMessage().isAtAll()) {
                            sbb.append("[有人@我] " + item.getMessage().getSender().getName() + " " + item.getMessage().getText())
                                    .setSpan(new ForegroundColorSpan(
                                                    Utils.getApp().getResources().getColor(R.color.carnation)),
                                            0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

                        }

                    } else if (hasExitAtMessage) {
                        sbb.append("[有人@我] " + isRoomThenAddName(item) + item.getMessage().getText())
                                .setSpan(new ForegroundColorSpan(
                                                Utils.getApp().getResources().getColor(R.color.carnation)),
                                        0, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    } else {
                        sbb.append(isRoomThenAddName(item) + item.getMessage().getText());
                    }
                    break;
                }
                case image: {
                    sbb.append(isRoomThenAddName(item) + "[图片]");
                    break;
                }
                case walkieTalkie: {
                    sbb.append(isRoomThenAddName(item) + "[语音]");
                    break;
                }
                case invite: {
                    sbb.append("我邀请你参加" + item.getMessage().getRoom().getName() + "房间");
                    break;
                }
                case checkIn: {
                    sbb.append(isRoomThenAddName(item) + "[签到]");
                    break;
                }
                default: {
                    sbb.append("[其他]");
                    break;
                }
            }
            return SpannableString.valueOf(sbb);
        }

        private static String isRoomThenAddName(Dialog item) {
            if (!item.isValid()) {
                return "";
            }
            if (item.getMessage().getSender() != null) {
                return item.isRoom() ? item.getMessage().getSender().getName() + "：" : "";
            }
            return "";
        }

        
        public static String getLatestTime(Dialog item) {
            if (!item.isValid()) {
                return "";
            }
            if (item.getMessage() == null) {
                return "";
            }
            return DateUtil.format(DateUtil.MESSAGE_PATTERN, item.getMessage().getCreated());
        }

        public static String getNotificationTime(NotificationViewModel systemNotification) {
            return DateUtil.format(DateUtil.MESSAGE_PATTERN, systemNotification.getCreated());
        }

        public static int getUnreadVisibility(Dialog item) {
            if (!item.isValid()) {
                return View.GONE;
            }
            return item.getUnreadCount() == 0 ? View.GONE : View.VISIBLE;
        }
    }
}
